﻿using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections.Generic;

namespace Frontend
{
    class StateSerializer
    {
        public static void SaveState(IStateSerializable root, string filename)
        {
            StateSerializer _this = new StateSerializer();

            IFormatter formatter = new BinaryFormatter();
            using (Stream stream = 
                    new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                formatter.Serialize(stream, root.GetState());
            }
            
        }

        public static void LoadState(IStateSerializable root, string filename)
        {
            IFormatter formatter = new BinaryFormatter();
            using (Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                
                StateDictionary state = (StateDictionary)formatter.Deserialize(stream);
                root.SetState(state);
            }                        
        }
    }
}
